package com.kongfanyu.springcloud.controller;
//中介者接口，定义各个同事对象通信的接口
interface Mediator{
	/**
	 * 同事对象在自身改变的时候来通知中介者的方法
	 * 让中介者去负责相应的与其他同事对象的交互
	 * @param colleague 同事对象自身，好让中介者对象通过对象实例去获取同事对象的状态
	 */
	public void changed(Colleage colleague);
	
}
abstract class Colleage{
	private Mediator mediator;
	public Colleage(Mediator mediator) {
		this.mediator = mediator;
	}
	
	public Mediator getMediator() {
		return this.mediator;
	}
}
//具体同事类
class CDDriver extends Colleage{
	public CDDriver(Mediator mediator) {
		super(mediator);
	}
	private String data = "";//光驱读取的数据
	/**
	 * 获取光驱读取出来的数据
	 * @return 光驱读取出来的数据
	 */
	public String getData() {
		return this.data;
	}
	//读取光盘：业务方法，也是和中介者交互的方法
	public void readCD() {
		this.data = "设计模式,值得好好研究";
		this.getMediator().changed(this);
	}
}
//CPU同事类
class CPU extends Colleage{
	public CPU(Mediator mediator) {
		super(mediator);
	}
	private String videoData;//解析的视频数据
	private String soundData;//解析的音频数据
	public String getVideoData() {
		return videoData;
	}
	public String getSoundData() {
		return soundData;
	}
	//处理数据，把数据分成音频和视频数据
	public void executeData(String data) {
		//把数据分解开，前面的是视频，后面的是音频
		String [] ss = data.split(",");
		this.videoData = ss[0];
		this.soundData = ss[1];
		//通知主板，CPU的工作完成后
		this.getMediator().changed(this);
	}
}
//显卡同事类
class VideoCard extends Colleage{
	public VideoCard(Mediator mediator) {
		super(mediator);
	}
	//显示数据
	public void showData(String data) {
		System.out.println("您正观看的是:"+data);
	}
}
//声卡同事类
class SoundCard extends Colleage{

	public SoundCard(Mediator mediator) {
		super(mediator);
	}
	//发出声音
	public void soundData(String data) {
		System.out.println("画外音:"+data);
	}
}
//主板类，实现中介者接口
class MotherBoard implements Mediator{
	private CDDriver cdDriver;
	private CPU cpu;
	private VideoCard videoCard;
	private SoundCard soundCard;
	
	public void setCdDriver(CDDriver cdDriver) {
		this.cdDriver = cdDriver;
	}

	public void setCpu(CPU cpu) {
		this.cpu = cpu;
	}

	public void setVideoCard(VideoCard videoCard) {
		this.videoCard = videoCard;
	}

	public void setSoundCard(SoundCard soundCard) {
		this.soundCard = soundCard;
	}

	@Override
	public void changed(Colleage colleague) {
		if(colleague == cdDriver) {
			//表示光驱读取数据了
			this.openCDDriverReadData((CDDriver) colleague);
		}else if(colleague == cpu) {
			//表示CPU处理完了
			this.openCUP((CPU) colleague);
		}
		
	}
	//处理光驱读取数据以后与其他对象的交互
	private void openCDDriverReadData(CDDriver cdDriver) {
		//1.先获取光驱读取的数据
		String data = cdDriver.getData();
		//2.把这些数据传递个CPU进行处理
		this.cpu.executeData(data);
	}
	//处理CPU处理完数据后与其他对象的交互
	private void openCUP(CPU cpu) {
		//1.先获取CPU处理后的数据
		String videoData = cpu.getVideoData();
		String soundData = cpu.getSoundData();
		//2.把这些数据传递给显卡和声卡显示出来
		this.videoCard.showData(videoData);
		this.soundCard.soundData(soundData);
	}
	
}
public class MediatorPatternDemo {

	public static void main(String[] args) {
		//1.创建中介者——主板对象
		MotherBoard mediator = new MotherBoard();
		//2.创建同事类
		CDDriver cd = new CDDriver(mediator);
		CPU cpu = new CPU(mediator);
		VideoCard vc = new VideoCard(mediator);
		SoundCard sc = new SoundCard(mediator);
		//3.让中介者知道所有的同事
		mediator.setCdDriver(cd);
		mediator.setCpu(cpu);
		mediator.setVideoCard(vc);
		mediator.setSoundCard(sc);
		//4.开始看电影，把光盘放入光驱，光驱开始读盘
		cd.readCD();

	}

}
